<template>
  <div style="padding: 10px">
    <!-- 功能区 -->
    <div style="margin：10px">
      <el-button type="primary" v-if="user.identity == 'admin'" @click="handleAdd">新增</el-button>
      <div class="search-right">
        <!-- <el-input
          v-model="search"
          placeholder="请输入搜索关键字"
          style="width: 300px"
        ></el-input>
        <el-button type="primary" style="margin-left: 5px">查询</el-button> -->
        <!-- 按照时间筛选 -->
        <el-form :inline="true" :model="search_data">
          <el-form-item label="按时间筛选：">
            <el-date-picker
              v-model="search_data.startTime"
              type="datetime"
              placeholder="选择开始时间"
            >
            </el-date-picker>
            -
            <el-date-picker
              v-model="search_data.endTime"
              type="datetime"
              placeholder="选择结束时间"
            >
            </el-date-picker>
            <el-button
              type="primary"
              style="margin-left: 5px"
              @click="handleSearch()"
              >查询</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>
    <!-- 搜索区 -->

    <!-- 表格数据 -->
    <div class="table-container">
      <el-table :data="tableData" stripe style="width: 100%" :border="true">
        <el-table-column type="index" width="50"> </el-table-column>
        <el-table-column prop="date" label="日期" sortable> </el-table-column>
        <el-table-column prop="type" label="类型"> </el-table-column>
        <el-table-column prop="describe" label="收支描述"> </el-table-column>
        <el-table-column prop="income" label="收入">
          <template slot-scope="scope">
            <span style="color: #00d003">{{ scope.row.income }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="expend" label="支出">
          <template slot-scope="scope">
            <span style="color: #f56767">{{ scope.row.expend }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="cash" label="账户现金">
          <template slot-scope="scope">
            <span style="color: #00300d">{{ scope.row.cash }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注"> </el-table-column>
        <el-table-column v-if="user.identity == 'admin'" label="操作" fixed="right" width="180">
          <template slot-scope="scope">
            <el-button
              @click="handleEdit(scope.$index, scope.row)"
              type="primary"
              >编辑</el-button
            >
            <el-button
              @click="handleDelete(scope.$index, scope.row)"
              type="danger"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="paginations.page_index"
        :page-sizes="paginations.page_sizes"
        :page-size="paginations.page_size"
        :layout="paginations.layout"
        :total="paginations.total"
      >
      </el-pagination>
    </div>
    <FundDialog
      :fundDialog="fundDialog"
      :form="form"
      @update="getProfile"
    ></FundDialog>
  </div>
</template>

<script>
import FundDialog from "@/components/FundDialog";
export default {
  name: "fundList",
  components: {
    FundDialog,
  },
  computed:{
    user() {
      return this.$store.getters.user;
    }
  },
  data() {
    return {
      //搜索
      search_data: {
        startTime: "",
        endTime: "",
      },
      filterTableData:[],
      //分页
      paginations: {
        page_index: 1, //当前位于哪一页
        total: 0, //总数
        page_size: 5, //一页显示多少条
        page_sizes: [5, 10, 20], //每页显示多少条
        layout: "total,sizes,prev,pager,next,jumper", //翻页属性
      },
      tableData: [],
      allTableData: [],
      form: {
        type: "",
        describe: "",
        income: "",
        expend: "",
        cash: "",
        remark: "",
        id: "",
      },
      fundDialog: {
        show: false,
        title: "",
        option: "edit",
      },
    };
  },
  created() {
    this.getProfile();
  },
  methods: {
    //获取资金信息
    getProfile() {
      this.$axios
        .get("/api/profile")
        .then((res) => {
          this.allTableData = res.data;
          this.filterTableData = res.data;
          //设置分页数据
          this.setPaginations();
        })
        .catch((err) => console.log(err));
    },
    //时间筛选
    handleSearch() {
      //筛选
      //判断开始，结束时间是否为空
      if(!this.search_data.startTime || ! this.search_data.endTime) {
        this.$message ({
          type:"warning",
          message: "请选择时间区间"
        });
        this.getProfile();
        return ;
      }
      const sTime = this.search_data.startTime.getTime();
      const eTime = this.search_data.endTime.getTime();
      this.allTableData = this.filterTableData.filter(item => {
        // console.log(item);
        let date = new Date(item.date);
        let time = date.getTime();
        return time >= sTime && time <= eTime;
      });
      //分页数据
      this.setPaginations();
    },
    //添加
    handleAdd() {
      this.fundDialog = {
        show: true,
        title: "添加资金信息",
        option: "add",
      };
      this.form = {
        type: "",
        describe: "",
        income: "",
        expend: "",
        cash: "",
        remark: "",
        id: "",
      };
      this.fundDialog.show = true;
    },
    // 编辑
    handleEdit(index, row) {
      this.fundDialog = {
        show: true,
        title: "修改资金信息",
        option: "edit",
      };
      this.form = {
        type: row.type,
        describe: row.describe,
        income: row.income,
        expend: row.expend,
        cash: row.cash,
        remark: row.remark,
        id: row._id,
      };
    },
    //删除
    handleDelete(index, row) {
      this.$axios.delete(`/api/profile/delete/${row._id}`).then((res) => {
        this.$message("删除成功");
        this.getProfile();
      });
    },
    // 分页
    setPaginations() {
      //分页属性
      this.paginations.total = this.allTableData.length;
      this.paginations.page_index = 1;
      this.paginations.page_size = 5;
      //分页数据
      this.tableData = this.allTableData.filter((item, index) => {
        return index < this.paginations.page_size;
      });
    },
    handleSizeChange(page_size) {
      //切换size
      this.paginations.page_index = 1;
      this.paginations.page_size = page_size;
      this.tableData = this.allTableData.filter((item, index) => {
        return index < page_size;
      });
    },
    handleCurrentChange(page) {
      //获取当前页
      let index = this.paginations.page_size * (page - 1);
      //数据总数
      let nums = this.paginations.page_size * page;
      let tables = [];
      for (let i = index; i < nums; i++) {
        if (this.allTableData[i]) {
          tables.push(this.allTableData[i]);
        }
        this.tableData = tables;
      }
    },
  },
};
</script>
<style lang="less">
.search-right {
  float: right;
}
.table-container {
  margin-top: 10px;
}
</style>
